#coding=utf-8
#!/usr/bin/python

'''
Created on 2013-5-20

@author: Neo.Chen
'''

import os
def copy_mg():
    print "产生mg多进程部署文件"
    xml = file("/opt/xml.txt", "w")
    sh = file("/opt/sh.txt", "w")
    
    for i in range(100, 150):
        strIndex = '%d' %i
        dstdir = "/opt/bin" + strIndex
        print dstdir
        os.system("rm -r -f " + dstdir)
        
        os.system("mkdir " + dstdir)
        os.system("cp -r /opt/bin2/* " + dstdir)
        os.system("mv " + dstdir + "/vcs_mg_2 " + dstdir + "/vcs_mg_" + strIndex)
		
        #写文件
        f = file(dstdir + "/conf/skycfg.ini", "w")
        f.write("[MGSvr]\r\n")
        f.write("MGSvrIP=0.0.0.0\r\n")
        f.write("CmdPort=" + '%d' %(i*2+20000) + "\r\n")
        f.write("DataPort=" + '%d' %(i*2+1+20000) + "\r\n")
        f.write("CodecCFGfile=./conf/mg_codec_cfg.xml\r\n")
        f.write("saveAV=false\r\n")
        f.close()
        
        #写文件
        f = file(dstdir + "/mgstart.sh", "w")
        f.write("export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./\n")
        f.write("ulimit -HSn 100000\n")
        f.write("ulimit -c 10000\n")
        f.write("./vcs_mg_" + '%d' %i + "&\n")
        f.close()

        #写文件
        xml.write("<mg_program_info>\n")
        xml.write("     <mg_ip>192.168.104.44</mg_ip>\n")
        xml.write("     <mg_port>" + '%d' %(i*2+20000) + "</mg_port>\n")
        xml.write("     <mg_enable>1</mg_enable>\n")
        xml.write("</mg_program_info>\n")
        sh.write("\n")
        
        #写文件
        sh.write("cd " + dstdir + "\n")
        sh.write("./mgstart.sh\n")
        sh.write("\n")
        
    xml.close()
    sh.close()


if __name__ == '__main__':
    copy_mg()
